package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "参观预约管理")
public class CustomerReservationController extends BaseController {

    @Autowired
    private CustomerReservationService crs;

    @GetMapping("/cancelled-count")
    @ApiOperation(value = "查询取消预约数量")
    public ResponseResult<Integer> getCancelledCount(){
        Integer count = crs.getCancelledCount();
        return success(count);
    }

    @GetMapping("/countByTime")
    @ApiOperation(value = "查询每个时间段的剩余预约次数")
    public ResponseResult<List<ReservationVo>> getCountByTime(Long time){
        Instant parse = Instant.ofEpochMilli(time);
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime dateTime = LocalDateTime.ofInstant(parse,zoneId);
        List<ReservationVo> list =  crs.getCountByTime(dateTime);
        return success(list);
    }

    @PostMapping
    @ApiOperation(value = "新增预约")
    public ResponseResult addReservation(@RequestBody ReservationDto reservationDto){
        crs.addReservation(reservationDto);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation(value = "分⻚查询预约")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "integer", paramType = "query", defaultValue = "1", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "integer", paramType = "query", defaultValue = "10", required = true),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "integer", paramType = "query", required = false)
    })
    public ResponseResult<PageResponse<ReservationVo>> selectByPage(Integer pageNum,Integer pageSize,Integer status){
        PageResponse<ReservationVo> page = crs.selectByPage(pageNum,pageSize,status);
        return success(page);
    }

    @PutMapping("/{id}/cancel")
    @ApiOperation(value = "取消预约")
    public ResponseResult cancle(@PathVariable Long id){
        crs.cancle(id);
        return success();
    }


}
